#include "StdAfx.h"
//-------------------------------------------------------------------------------------------------
//ArchiveProcGUIDoc.cpp : implementation of the CArchiveProcGUIDoc class        
//-------------------------------------------------------------------------------------------------
//FreeBSD License
//Copyright 2011 Will Stott All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are
//permitted provided that the following conditions are met:
//
//   1. Redistributions of source code must retain the above copyright notice, this list of
//      conditions and the following disclaimer.
//
//   2. Redistributions in binary form must reproduce the above copyright notice, this list
//      of conditions and the following disclaimer in the documentation and/or other materials
//      provided with the distribution.
//
//THIS SOFTWARE IS PROVIDED BY Will Stott ``AS IS'' AND ANY EXPRESS OR IMPLIED
//WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
//FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Will Stott OR
//CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
//ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
//ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//The views and conclusions contained in the software and documentation are those of the
//authors and should not be interpreted as representing official policies, either expressed
//or implied, of Will Stott.
//
//----Authors--------------------------------------------------------------------------------------
//created by: will.stott@ucl.ac.uk July 2011
//
//----Comments-------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------

// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "ArchiveProcGUI.h"
#endif

#include "ChildFrm.h"
#include "ArchiveProcGUIDoc.h"
#include "MainFrm.h"
#include "ToolsOptionsDlg.h"
#include "FileNewDlg.h"
#include "cstringt.h"

#include "SelectType.h"
#include "PtShapeType.h"
#include "PtAnnotateShapeType.h"
#include "EllipseShapeType.h"

#include "SelectTool.h"

#include <propkey.h>



#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CArchiveProcGUIDoc

IMPLEMENT_DYNCREATE(CArchiveProcGUIDoc, CDocument)

BEGIN_MESSAGE_MAP(CArchiveProcGUIDoc, CDocument)
	ON_COMMAND(ID_TOOLS_OPTIONS, &CArchiveProcGUIDoc::OnToolsOptions)
	ON_COMMAND(ID_VIEWSETDEFAULTBRIGHTNESS, &CArchiveProcGUIDoc::OnViewSetDefaultBrightness)
	ON_UPDATE_COMMAND_UI(ID_VIEWSETDEFAULTBRIGHTNESS, &CArchiveProcGUIDoc::OnUpdateSetDefaultBrightness)
	ON_COMMAND(ID_VIEWSETDEFAULTCONTRAST, &CArchiveProcGUIDoc::OnViewSetDefaultContrast)
	ON_UPDATE_COMMAND_UI(ID_VIEWSETDEFAULTCONTRAST, &CArchiveProcGUIDoc::OnUpdateSetDefaultContrast)
	ON_COMMAND(ID_VIEW_HIDEIMAGEOBJECTS, &CArchiveProcGUIDoc::OnViewHideImageObjects)
	ON_UPDATE_COMMAND_UI(ID_VIEW_HIDEIMAGEOBJECTS, &CArchiveProcGUIDoc::OnUpdateViewHideImageObjects)
END_MESSAGE_MAP()


// CArchiveProcGUIDoc construction/destruction

CArchiveProcGUIDoc::CArchiveProcGUIDoc() : 
	m_pIdfDoc('\0'),
	m_pSelImageProps('\0'),
	m_pSelTool('\0'),
	m_HideImageObjects(false)
{
//!!!!!!!!!!!!!!!
#ifdef _DEBUG
	MxUtils::OutputDebugMsg("CArchiveProcGUIDoc::CArchiveProcGUIDoc()", "document ctor");
#endif
//!!!!!!!!!!!!!!!

	m_pIdfDoc = new ImageDataset(); 
	m_pSelImageProps = new ImageProps();

	m_ResourceIDList.Add(SelectType::baseClassName, IDS_SELECTTYPE, IDS_SELECTTYPE);
	m_ResourceIDList.Add(PtShapeType::baseClassName, IDS_PTSHAPETYPE_1, IDS_PTSHAPETYPE_10);
	m_ResourceIDList.Add(EllipseShapeType::baseClassName, IDS_ELLIPSETYPE_1, IDS_ELLIPSETYPE_10);
	m_ResourceIDList.Add(PtAnnotateShapeType::baseClassName, IDS_PTANNOTATEETYPE_1, IDS_PTANNOTATEETYPE_10);
}

CArchiveProcGUIDoc::~CArchiveProcGUIDoc()
{
//!!!!!!!!!!!!!!!
#ifdef _DEBUG
	MxUtils::OutputDebugMsg("CArchiveProcGUIDoc::~CArchiveProcGUIDoc()", "document dtor");
#endif
//!!!!!!!!!!!!!!!

	if (m_pIdfDoc != '\0')
		delete m_pIdfDoc;
	if (m_pSelImageProps != '\0')
		delete m_pSelImageProps;
}

CArchiveProcGUIDoc *CArchiveProcGUIDoc::GetActiveDoc()
{
	CArchiveProcGUIDoc *rc = '\0';

	//CMainFrame *pMainWnd = dynamic_cast<CMainFrame *>(AfxGetMainWnd());
	//if (pMainWnd != '\0')
	//{
	//	CView* pView = pMainWnd->GetActiveView(); 
	//	if (pView != '\0')
	//		rc = dynamic_cast<CArchiveProcGUIDoc *>(pView->GetDocument());
	//}

	//CMainFrame *pMainWnd = dynamic_cast<CMainFrame *>(AfxGetMainWnd());
	//if (pMainWnd != '\0')
	//{
	//	CFrameWnd* pFrame = pMainWnd->GetActiveFrame( );
	//	if (pFrame != '\0')
	//		rc = dynamic_cast<CArchiveProcGUIDoc *>(pFrame->GetActiveDocument());
	//}

	CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->GetMainWnd();
	if (pFrame != NULL )
	{
		CMDIChildWnd *pChild = pFrame->MDIGetActive();
		if (pChild != NULL )
		{
			CView *pView = (CView*)pChild->GetActiveView();
			if (pView != NULL)
				rc = dynamic_cast<CArchiveProcGUIDoc *>(pView->GetDocument());
		}
	}
	return rc;
}


BOOL CArchiveProcGUIDoc::OnNewDocument()
{
	bool rc = false;

//!!!!!!!!!!!!!!!
#ifdef _DEBUG
	MxUtils::OutputDebugMsg("CArchiveProcGUIDoc::OnNewDocument()", "new document initialisation");
#endif
//!!!!!!!!!!!!!!!

	if (CDocument::OnNewDocument())
	{
		CMainFrame *pMainFrame = dynamic_cast<CMainFrame *>(AfxGetMainWnd());
		if ( (pMainFrame != '\0') && (pMainFrame->IsKindOf(RUNTIME_CLASS(CMainFrame))))
		{
			pMainFrame->ShowPropertiesWnd(false);
			//rc = true;
		}

		CArchiveProcGUIApp *pApp = dynamic_cast<CArchiveProcGUIApp *>(AfxGetApp());
		if (pApp != '\0')
		{
			FileNewDlg dlg;

			dlg.m_RemoteDbConnectionStr = pApp->GetDbConnection(L"DbConnectLabel-OCUDSDB");
			dlg.m_LocalDbConnectionStr = pApp->GetDbConnection(L"DbConnectLabel-HPDEV");

			dlg.m_ImageDatasetFile = pApp->GetImageDatasetFile();
			dlg.m_DataDir = pApp->GetDataDir();
			dlg.m_SQLStatementFile = pApp->GetSQLStatementFile();
			dlg.m_ImageSource = pApp->GetImageSource();
			dlg.m_ImportImagesDir = pApp->GetImportImagesDir();
			dlg.m_IDFName = pApp->GetIDFName();
			dlg.m_IDFOwner = pApp->GetIDFOwner();
			dlg.m_IDFDescription = pApp->GetIDFDescription();
			dlg.m_IDFVersion = pApp->GetIDFVersion();
			dlg.m_IDFRepository = pApp->GetIDFRepository();

			if (dlg.DoModal() == IDOK)
			{
					pApp->SetImageDatasetFile(dlg.m_ImageDatasetFile);
					pApp->SetDataDir(dlg.GetDataDir());
					pApp->SetSQLStatementFile(dlg.m_SQLStatementFile);
					pApp->SetImageSource(dlg.m_ImageSource);
					pApp->SetImportImagesDir(dlg.m_ImportImagesDir);
					pApp->SetIDFName(dlg.m_IDFName);
					pApp->SetIDFOwner(dlg.m_IDFOwner);
					pApp->SetIDFDescription(dlg.m_IDFDescription);
					pApp->SetIDFVersion(dlg.m_IDFVersion);
					pApp->SetIDFRepository(dlg.m_IDFRepository);

					CString  IDFfile(dlg.GetIDFPathFilename());

					if (OnOpenDocument(IDFfile))
						rc = true;

			}
		}
	}
	return rc;
}


BOOL CArchiveProcGUIDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
	BOOL rc = FALSE;

//!!!!!!!!!!!!!!!
#ifdef _DEBUG
	MxUtils::OutputDebugMsg("CArchiveProcGUIDoc:::OnSaveDocument()", "save document" );
#endif
//!!!!!!!!!!!!!!!

	if (m_pIdfDoc != '\0')
	{
		DoUpdateAllViews(NULL, CArchiveProcGUIDoc::UpdateAllViewsSave, NULL);

		if ( m_pIdfDoc->SavePropertiesForImage(m_pSelImageProps) )
		{
			if ( m_pIdfDoc->SaveFile(lpszPathName) )
			{
				SetModifiedFlag(FALSE); //saving ImageDataSet should mean all of its components are no longer changed
				rc = TRUE;
			}
		}
	}
	if ( rc == FALSE)
		CArchiveProcGUIApp::UserErrorMsg("System Error", "failed to save file", "successful", "Select a different file for saving your work (File | SaveAs) and try again");

	return rc;
}



BOOL CArchiveProcGUIDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
	BOOL rc = FALSE;

//!!!!!!!!!!!!!!!
#ifdef _DEBUG
	MxUtils::OutputDebugMsg("CArchiveProcGUIDoc::OnOpenDocument()", "ENTRY: Open [%s]", MxUtils::WideCharsToAscii_1(lpszPathName));
#endif
//!!!!!!!!!!!!!!!
	bool Abort = false;
	CMainFrame *pMainFrame = dynamic_cast<CMainFrame *>(AfxGetMainWnd());
	if ( (pMainFrame != '\0') && (pMainFrame->IsKindOf(RUNTIME_CLASS(CMainFrame))))
	{
		if (pMainFrame->IsSelectedDoc('\0') == false)
		{
			CArchiveProcGUIApp::UserErrorMsg("User Error", "Unable to open more than one IDF at a time", "successful", "Close current IDF and try again");
			Abort = true;
		}
	}

	if ( (Abort == false) && (pMainFrame != '\0') && (m_pIdfDoc != '\0') && (m_pSelImageProps != '\0'))
	{
		if ( m_pIdfDoc->LoadFile(lpszPathName, m_ResourceIDList) == false)
			CArchiveProcGUIApp::UserErrorMsg("System Error", "failed to open file. File is corrupt or bad", "successful", "Select a different file and try again");
		else
		{
			ImageObjectType *pImagObjType = m_pIdfDoc->StartImageObjectTypeIter();
			while ((pImagObjType = m_pIdfDoc->GetNextImageObjectType()) != '\0')
				m_pSelImageProps->AddImageObjectTypeInfo(pImagObjType->GetName());

			if (m_DrawingTools.CreateImageObjectTypeTools(m_pIdfDoc))
			{
				SelectTool *pSelect = dynamic_cast<SelectTool *>(m_DrawingTools.GetToolForImageObjectTypeName (SelectType::baseClassName));
				if (pSelect != '\0')
				{
					pSelect->SetDrawingTools(&m_DrawingTools);

					pMainFrame->ShowPropertiesWnd(true);
					rc = TRUE;
				}
			}
		}
	}
//!!!!!!!!!!!!!!!
#ifdef _DEBUG
	MxUtils::OutputDebugMsg("CArchiveProcGUIDoc::OnOpenDocument()", "EXIT rc=%d", (rc == TRUE) ? 1 : 0);
#endif
//!!!!!!!!!!!!!!!

	return rc;
}

void CArchiveProcGUIDoc::OnCloseDocument()
{
//!!!!!!!!!!!!!!!
#ifdef _DEBUG
	MxUtils::OutputDebugMsg("CArchiveProcGUIDoc::OnCloseDocument()", "document closing");
#endif
//!!!!!!!!!!!!!!!

	CMainFrame *pMainFrame = dynamic_cast<CMainFrame *>(AfxGetMainWnd());
	if ( (pMainFrame != '\0') && (pMainFrame->IsKindOf(RUNTIME_CLASS(CMainFrame))))
	{
		pMainFrame->UpdatePropertiesWnd(m_pSelImageProps, CArchiveProcGUIDoc::UpdateAllViewsCloseDoc);
		pMainFrame->ShowPropertiesWnd(false);
		pMainFrame->SetSelectedDoc('\0');
	}

	CDocument::OnCloseDocument();
}



void CArchiveProcGUIDoc::OnViewHideImageObjects()
{
	m_HideImageObjects = (m_HideImageObjects == true) ? false :  true;
	UpdateAllViews(NULL, CArchiveProcGUIDoc::UpdateAllViewsChangeImageObjectDisplay);
}


void CArchiveProcGUIDoc::OnUpdateViewHideImageObjects(CCmdUI *pCmdUI)
{
	if (m_pSelImageProps == '\0')
		pCmdUI->Enable(FALSE);
	else
	{
		if (m_pSelImageProps->GetImageObjectCnt() <= 0 )
			pCmdUI->Enable(FALSE);
		else
		{
			pCmdUI->Enable(TRUE);
		}
	}
	pCmdUI->SetText( ( m_HideImageObjects == true ) ? L"Show Image Objects" : L"Hide Image Objects");
}


void CArchiveProcGUIDoc::OnViewSetDefaultBrightness()
{
	DoChangeBrightness(atoi(ImageProps::BrightnessMid));
}


void CArchiveProcGUIDoc::OnUpdateSetDefaultBrightness(CCmdUI *pCmdUI)
{
	if (m_pSelImageProps == '\0')
		pCmdUI->Enable(FALSE);
	else
	{
		if ( m_pSelImageProps->GetBrightnessValue() == atoi(ImageProps::BrightnessMid) )
			pCmdUI->Enable(FALSE);
		else
			pCmdUI->Enable(TRUE);
	}
}


void CArchiveProcGUIDoc::OnViewSetDefaultContrast()
{
	DoChangeContrast(atoi(ImageProps::ContrastMid)); 
}


void CArchiveProcGUIDoc::OnUpdateSetDefaultContrast(CCmdUI *pCmdUI)
{
	if (m_pSelImageProps == '\0')
		pCmdUI->Enable(FALSE);
	else
	{
		if ( m_pSelImageProps->GetContrastValue() == atoi(ImageProps::ContrastMid) )
			pCmdUI->Enable(FALSE);
		else
			pCmdUI->Enable(TRUE);
	}
}

void	CArchiveProcGUIDoc::DoUpdateAllViews(CWnd *pSender, int UpdateAllViewslHint, CObject *pObj)
{
	//called by CPictureCtrl::RefreshImageObjectArea() when user clicks left mouse button, drags and releases
	//called by CPropertiesWnd::OnImageTypeSelChange()
//!!!!!!!!!!!!!!!
#ifdef _DEBUG
	if (  UpdateAllViewslHint != CArchiveProcGUIDoc::UpdateAllViewsUpdateImageObject )
		MxUtils::OutputDebugMsg("CArchiveProcGUIDoc::DoUpdateAllViews()", "UpdateAllViewslHint: %s", CArchiveProcGUIDoc::GetUpdateHintStr((LPARAM)UpdateAllViewslHint));
#endif
//!!!!!!!!!!!!!!!

	CView *pView = '\0';
	if ( (pSender != '\0') && (pSender->IsKindOf(RUNTIME_CLASS(CView))))
		pView = dynamic_cast<CView *>(pSender);

	bool UpdateDone = false;
	if ( (UpdateAllViewslHint ==  CArchiveProcGUIDoc::UpdateAllViewsSave) || (UpdateAllViewslHint ==  CArchiveProcGUIDoc::UpdateAllViewsCompleteImageObject))
	{
		UpdateAllViews(pView, (LPARAM)CArchiveProcGUIDoc::UpdateAllViewsSave, pObj);	//tell all views to save information from their UI into ImageProps
		UpdateDone = true;

		CMainFrame *pMainFrame = dynamic_cast<CMainFrame *>(AfxGetMainWnd());
		if ( (pMainFrame != '\0') && (pMainFrame->IsKindOf(RUNTIME_CLASS(CMainFrame))))
		{
			if ( pMainFrame->SavePropertiesWnd(m_pSelImageProps))
				SetModifiedFlag(TRUE);			
			
			if (UpdateAllViewslHint ==  CArchiveProcGUIDoc::UpdateAllViewsCompleteImageObject)
			{
				CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->GetMainWnd();
				if (pFrame != NULL )
				{
					CChildFrame  *pChild =  dynamic_cast<CChildFrame *>(pFrame->MDIGetActive());
					if (pChild != NULL )
					{
						SelectDymanicMenu(IDS_SELECTTYPE);
					}
				}
			}
		}
	}
	else if ( (UpdateAllViewslHint ==  CArchiveProcGUIDoc::UpdateAllViewsCreateImageObject ) || (UpdateAllViewslHint ==  CArchiveProcGUIDoc::UpdateAllViewsDeleteImageObject))
	{
		CMainFrame *pMainFrame = dynamic_cast<CMainFrame *>(AfxGetMainWnd());
		if ( (pMainFrame != '\0') && (pMainFrame->IsKindOf(RUNTIME_CLASS(CMainFrame))))
		{
			pMainFrame->LoadPropertiesWnd(m_pSelImageProps, m_pSelTool, m_pIdfDoc);	//image properties have changed, so update prop windows
		}
	}
	else if (UpdateAllViewslHint == CArchiveProcGUIDoc::UpdateAllViewsSetNewImageType)
	{
		CMainFrame *pMainFrame = dynamic_cast<CMainFrame *>(AfxGetMainWnd());
		if ( (pMainFrame != '\0') && (pMainFrame->IsKindOf(RUNTIME_CLASS(CMainFrame))))
		{
			if ( pMainFrame->SavePropertiesWnd(m_pSelImageProps))
				SetModifiedFlag(TRUE);	
		}
	}

	if (UpdateDone == false)
	{
		UpdateAllViews(pView, (LPARAM)UpdateAllViewslHint, pObj);
	}
}

bool	CArchiveProcGUIDoc::DoChangeBrightness(int val)
{
	bool rc = false;

	//!!!!!!!!!!!!!!!
#ifdef _DEBUG
	MxUtils::OutputDebugMsg("CArchiveProcGUIDoc::DoChangeBrightness()", "change brightness - val(%d) and update all views", val );
#endif
//!!!!!!!!!!!!!!!

	if (m_pSelImageProps != '\0')
	{
		if (rc = m_pSelImageProps->SetBrightness(val))
		{			//todo replace this block with DoUpdateAllViews()
			CMainFrame *pMainFrame = dynamic_cast<CMainFrame *>(AfxGetMainWnd());
			if ( (pMainFrame != '\0') && (pMainFrame->IsKindOf(RUNTIME_CLASS(CMainFrame))))
			{
				 pMainFrame->UpdatePropertiesWnd(m_pSelImageProps, CArchiveProcGUIDoc::UpdateAllViewsSetBrightnessContrast);
			}
			SetModifiedFlag(TRUE);		
			UpdateAllViews(NULL, CArchiveProcGUIDoc::UpdateAllViewsSetBrightnessContrast);
		}
	}
	return rc;
}


bool	CArchiveProcGUIDoc::DoChangeContrast(int val)
{
	bool rc = false;

	if (m_pSelImageProps != '\0')
	{
		if (rc = m_pSelImageProps->SetContrast(val))
		{		//todo replace this block with DoUpdateAllViews()
			CMainFrame *pMainFrame = dynamic_cast<CMainFrame *>(AfxGetMainWnd());
			if ( (pMainFrame != '\0') && (pMainFrame->IsKindOf(RUNTIME_CLASS(CMainFrame))))
			{
				 pMainFrame->UpdatePropertiesWnd(m_pSelImageProps, CArchiveProcGUIDoc::UpdateAllViewsSetBrightnessContrast);
			}
			SetModifiedFlag(TRUE);		
			UpdateAllViews(NULL, CArchiveProcGUIDoc::UpdateAllViewsSetBrightnessContrast);
		}
	}

	return rc;
}


bool CArchiveProcGUIDoc::DoSelectImage( CTreeView *pView, const char *DicomRef)  
{
	bool rc = false;

//!!!!!!!!!!!!!!!
#ifdef _DEBUG
	MxUtils::OutputDebugMsg("CArchiveProcGUIDoc::DoSelectImage()", "called from CArchiveProcGUIView::OnTvnSelchanged() or CArchiveProcGUIView::OnViewRefresh() when image selected: %s", ImageProps::IsValidDicomRef(DicomRef) ? DicomRef : "DicomRef is null or empty" );
#endif
//!!!!!!!!!!!!!!!

	if ( m_pIdfDoc != '\0') 
	{
		DoUpdateAllViews(NULL, CArchiveProcGUIDoc::UpdateAllViewsSave, NULL);	

		if ( m_pIdfDoc->SavePropertiesForImage(m_pSelImageProps) )
		{
			CMainFrame *pMainFrame = dynamic_cast<CMainFrame *>(AfxGetMainWnd());
			if ( (pMainFrame != '\0') && (pMainFrame->IsKindOf(RUNTIME_CLASS(CMainFrame))))
			{
				ImageObjectTypeTool *pSelTool = '\0';
				if (ImageProps::IsValidDicomRef(DicomRef) == false)
					rc = true;
				else
				{
					if ( m_pIdfDoc->LoadPropertiesForImage(DicomRef,  m_pSelImageProps, GetSelImageObjectTypeTool()))
					{
						pSelTool = m_DrawingTools.GetToolForImageObjectType(m_pIdfDoc->GetImageObjectTypeForMenuId(IDS_SELECTTYPE));
						rc = true;
					}
				}
				SetSelImageObjectTypeTool(pSelTool);

				pMainFrame->LoadPropertiesWnd(m_pSelImageProps, pSelTool, m_pIdfDoc);

				UpdateAllViews(pView, CArchiveProcGUIDoc::UpdateAllViewsSetNewImage); 
			}
		}
	}
	return rc;
}

bool	CArchiveProcGUIDoc::SetSelImageObjectTypeTool(ImageObjectTypeTool *pSelTool) 
{ 
	bool rc = false;

//!!!!!!!!!!!!!!!
#ifdef _DEBUG
	MxUtils::OutputDebugMsg("CArchiveProcGUIDoc::SetSelImageObjectTypeTool():ENTRY", "sets selected tool for Image Object Type %s", (pSelTool != '\0') ? pSelTool->GetImageObjectType()->GetName() : "pSelTool is null");
#endif
//!!!!!!!!!!!!!!!

	if ( (m_pSelTool = pSelTool ) != '\0')
		rc = true;

	if ( m_pSelImageProps != '\0')
	{
		bool resetSelImageObject = true;
		if ( rc == true)
			resetSelImageObject = (pSelTool->IsCompatible(m_pSelImageProps->GetSelImageObject())) ? false : true;

		if ( resetSelImageObject )
			m_pSelImageProps->SetSelImageObject(m_pSelImageProps->GetFirstImageObject(pSelTool));	
	}

//!!!!!!!!!!!!!!!
#ifdef _DEBUG
	MxUtils::OutputDebugMsg("CArchiveProcGUIDoc::SetSelImageObjectTypeTool():EXIT", "SetSelImageObject %s for selected tool %s", ((m_pSelImageProps != '\0')  && (m_pSelImageProps->GetSelImageObject() != '\0')) ? m_pSelImageProps->GetSelImageObject()->GetDisplayName() : "m_pSelImageProps or m_pSelImageProps->GetSelImageObject() is null", (pSelTool != '\0') ? pSelTool->GetImageObjectType()->GetName() : "pSelTool is null");
#endif
//!!!!!!!!!!!!!!!

	return rc; 
}

bool	CArchiveProcGUIDoc::SetSelImageProperties(ImageProps	 *pProp) 
{ 
	return (m_pSelImageProps = pProp) ? true : false; 
}

bool CArchiveProcGUIDoc::SetupDynamicMenus(CMFCPopupMenu* pMenuPopup, int iIndex)		
{
	bool rc = false;

	if ((m_pIdfDoc != '\0') && (m_pSelImageProps != '\0'))
	{
		rc = m_DrawingTools.SetupDynamicMenus(pMenuPopup, iIndex, m_pSelImageProps->GetSelImageType(), m_pIdfDoc);
	}
	return rc;
}

bool CArchiveProcGUIDoc::DrawImageDynamicMenu(CDC* pDC, const CMFCToolBarMenuButton* pMenuButton, const CRect& rectImage)
{
	bool rc = false;

	if (pMenuButton != '\0')
	{
		if (IsDynamicMenuId(pMenuButton->m_nID))
		{
			bool select = ((m_pSelTool != '\0') && (m_pSelTool->GetResourceId() == pMenuButton->m_nID)) ? true : false;
			rc = m_DrawingTools.DrawImageDynamicMenu(pDC, pMenuButton, rectImage, m_pIdfDoc, select);
		}
	}

	return rc;
}

bool CArchiveProcGUIDoc::SelectDymanicMenu(int ResourceId)
{
	bool rc = false;

	if ( m_pIdfDoc != '\0' )
	{
		ImageObjectTypeTool *pSelTool = '\0';
		if ( (pSelTool = m_DrawingTools.GetToolForImageObjectType(m_pIdfDoc->GetImageObjectTypeForMenuId(ResourceId))) != '\0')
		{
			if ((rc = SetSelImageObjectTypeTool(pSelTool)) == true)
				UpdateAllViews(NULL, CArchiveProcGUIDoc::UpdateAllViewsSetNewTool, NULL);
		}
	}
	return rc;
}


#ifdef SHARED_HANDLERS

// Support for thumbnails
void CArchiveProcGUIDoc::OnDrawThumbnail(CDC& dc, LPRECT lprcBounds)
{
	// Modify this code to draw the document's data
	dc.FillSolidRect(lprcBounds, RGB(255, 255, 255));

	CString strText = _T("TODO: implement thumbnail drawing here");
	LOGFONT lf;

	CFont* pDefaultGUIFont = CFont::FromHandle((HFONT) GetStockObject(DEFAULT_GUI_FONT));
	pDefaultGUIFont->GetLogFont(&lf);
	lf.lfHeight = 36;

	CFont fontDraw;
	fontDraw.CreateFontIndirect(&lf);

	CFont* pOldFont = dc.SelectObject(&fontDraw);
	dc.DrawText(strText, lprcBounds, DT_CENTER | DT_WORDBREAK);
	dc.SelectObject(pOldFont);
}

// Support for Search Handlers
void CArchiveProcGUIDoc::InitializeSearchContent()
{
	CString strSearchContent;
	// Set search contents from document's data. 
	// The content parts should be separated by ";"

	// For example:  strSearchContent = _T("point;rectangle;circle;ole object;");
	SetSearchContent(strSearchContent);
}

void CArchiveProcGUIDoc::SetSearchContent(const CString& value)
{
	if (value.IsEmpty())
	{
		RemoveChunk(PKEY_Search_Contents.fmtid, PKEY_Search_Contents.pid);
	}
	else
	{
		CMFCFilterChunkValueImpl *pChunk = NULL;
		ATLTRY(pChunk = new CMFCFilterChunkValueImpl);
		if (pChunk != NULL)
		{
			pChunk->SetTextValue(PKEY_Search_Contents, value, CHUNK_TEXT);
			SetChunkValue(pChunk);
		}
	}
}

#endif // SHARED_HANDLERS

// CArchiveProcGUIDoc diagnostics

#ifdef _DEBUG
void CArchiveProcGUIDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CArchiveProcGUIDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG


// CArchiveProcGUIDoc commands


void CArchiveProcGUIDoc::OnToolsOptions()
{
	ToolsOptionsDlg dlg;

	CArchiveProcGUIApp *pApp = dynamic_cast<CArchiveProcGUIApp *>(AfxGetApp());
	if (pApp != '\0')
	{
		dlg.m_DbRemoteConnection = pApp->GetDbConnection(L"DbConnectLabel-OCUDSDB");
		dlg.m_DbLocalConnection = pApp->GetDbConnection(L"DbConnectLabel-HPDEV");
		dlg.m_nAutoFileSavePeriod = pApp->GetAutoFileSavePeriod();

		if (dlg.DoModal() == IDOK )
		{
			 pApp->SetDbConnection(L"DbConnectLabel-HPDEV", dlg.m_DbLocalConnection);
			 pApp->SetDbConnection(L"DbConnectLabel-OCUDSDB", dlg.m_DbRemoteConnection);
			 pApp->SetAutoFileSavePeriod(dlg.m_nAutoFileSavePeriod);
		}
	}
}

const char   *CArchiveProcGUIDoc::GetUpdateHintStr(LPARAM lHint)
{
	const char *rc = "";

	switch(lHint)
	{
		case CArchiveProcGUIDoc::UpdateAllViewsMFCFramework:
		{
			rc = "UpdateAllViewsMFCFramework - MFC Framework has invoked UpdateAllViews";
			break;
		}
		case CArchiveProcGUIDoc::UpdateAllViewsSetNewImage:
		{
			rc = "UpdateAllViewsSetNewImage - new image selected in TreeView";
			break;
		}
		case CArchiveProcGUIDoc::UpdateAllViewsSetBrightnessContrast:
		{
			rc = "UpdateAllViewsSetBrightnessContrast - brightness or contrast changing for the current image";
			break;
		}
		case CArchiveProcGUIDoc::UpdateAllViewsSetNewImageType:
		{
			rc = "UpdateAllViewsSetNewImageType - image type changing for the current image";
			break;
		}
		case CArchiveProcGUIDoc::UpdateAllViewsSetNewImageObject:
		{
			rc = "UpdateAllViewsSetNewImageObject - selected image object changing for the current image";
			break;
		}
		case CArchiveProcGUIDoc::UpdateAllViewsSetNewTool:
		{
			rc = "UpdateAllViewsSetNewTool - drawing tool changing";
			break;
		}
		case CArchiveProcGUIDoc::UpdateAllViewsCreateImageObject:
		{
			rc = "UpdateAllViewsCreateImageObject - new image object created (mouse left button down) or deleted (Edit | Erase)";
			break;
		}
		case CArchiveProcGUIDoc::UpdateAllViewsUpdateImageObject:
		{
			rc = "UpdateAllViewsUpdateImageObject- image object updating - mouse drag";
			break;
		}
		case  CArchiveProcGUIDoc::UpdateAllViewsCompleteImageObject:
		{
			rc = "UpdateAllViewsCompleteImageObject - image object completed - mouse left button up";				
			break;
		}
		case  CArchiveProcGUIDoc::UpdateAllViewsDeleteImageObject:
		{
			rc = "UpdateAllViewsDeleteImageObject - image object delete - Edit | Erase";				
			break;
		}
		case  CArchiveProcGUIDoc::UpdateAllViewsOpenDoc:
		{
			rc = "UpdateAllViewsOpenDoc - load new image items from document";				
			break;
		}
		case  CArchiveProcGUIDoc::UpdateAllViewsCloseDoc:
		{
			rc = "UpdateAllViewsCloseDoc - clear property window";				
			break;
		}
		case  CArchiveProcGUIDoc::UpdateAllViewsSave:
		{
			rc = "UpdateAllViewsSave - tell all views to save changes in their UI controls";				
			break;
		}
		case  CArchiveProcGUIDoc::UpdateAllViewsChangeImageObjectDisplay:
		{
			rc = "UpdateAllViewsChangeImageObjectDisplay - View | Hide Image Objects";				
			break;
		}
		default:
		{
			rc = "(unknown lhint)";
			break;
		}
	}
	return rc;
}



//-------------------------------------------------------------------------------------------------
//			E N D   O F   F I L E
//-------------------------------------------------------------------------------------------------















